package com.jtmg.glorykingsnews.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jtmg.glorykingsnews.domain.TbBooth;
import com.jtmg.glorykingsnews.domain.TbManager;
import com.jtmg.glorykingsnews.domain.TbOrder;
import com.jtmg.glorykingsnews.mapper.TbBoothMapper;
import com.jtmg.glorykingsnews.mapper.TbManagerMapper;
import com.jtmg.glorykingsnews.vo.commom.LayuiReturnVo;
import com.jtmg.glorykingsnews.vo.commom.MsgVo;
import com.jtmg.glorykingsnews.vo.order.OrderResaultVo;
import com.jtmg.glorykingsnews.vo.order.QueryOrderVo;
import com.jtmg.glorykingsnews.vo.order.UpdateOrderVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.jtmg.glorykingsnews.mapper.TbOrderMapper;
import com.jtmg.glorykingsnews.service.TbOrderService;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TbOrderServiceImpl implements TbOrderService{

    @Resource
    private TbOrderMapper tbOrderMapper;
    @Resource
    private TbBoothMapper tbBoothMapper;
    @Resource
    private TbManagerMapper tbManagerMapper;

    @Override
    public MsgVo createOrder(TbOrder order) {

        try {
            order.setId(UUID.randomUUID().toString());   //这玩意原理是啥？如何保证生成的随机数不会重复？
            int boothId = order.getBoothId();
            BigDecimal price = tbBoothMapper.selectByPrimaryKey(boothId).getPrice();
            int lease = order.getLease();
            //把lease转换为BigDecimal类型进行乘积
            BigDecimal bLease = new BigDecimal(lease);
            BigDecimal totalPrice = price.multiply(bLease);
            order.setLease(lease);
            order.setTotalPrice(totalPrice);
            order.setOrderTime(new Date());
            order.setOrderStatus("1");
            order.setDeleted(0);
            tbOrderMapper.insert(order);
            return new MsgVo("200","生成订单成功");
        }
        catch (Exception e){
            return new MsgVo("201","生成订单失败");
        }
    }

    @Override
    public MsgVo<TbOrder> queryOrderByUserId(String id) {
        try{
            Example example = new Example(TbOrder.class);
            example.createCriteria().andEqualTo("deleted",0)
                    .andEqualTo("userId",id);
            List<TbOrder> orders = tbOrderMapper.selectByExample(example);
            return new MsgVo<TbOrder>("200","查询订单成功",orders);
        }catch (Exception e){
            return new MsgVo<TbOrder>("201","查询订单失败");

        }
    }

    @Override
    public LayuiReturnVo<OrderResaultVo> managerQueryOrder(QueryOrderVo vo) {

            //获取夜市的id
            int marketId = tbManagerMapper.selectByPrimaryKey(vo.getManagerId()).getMid();
            //查询该夜市下的所有订单(需要连接订单表和摊位表）  mapper还没写
            List<OrderResaultVo> orders = tbOrderMapper.managerQueryOrder(marketId,vo);
            int count = orders.size();
            System.out.println(marketId);
            return new  LayuiReturnVo<OrderResaultVo>(0,"查询订单成功",count,orders);

    }

    @Override
    public MsgVo updateOrder(UpdateOrderVo updateOrderVo) {

        try{
            //根据订单id查摊位id
            int boothId = tbOrderMapper.selectByPrimaryKey(updateOrderVo.getOrderId()).getBoothId();
            //根据摊位id查询摊位的单价
            BigDecimal price = tbBoothMapper.selectByPrimaryKey(boothId).getPrice();
            //修改后的总价
            int lease = updateOrderVo.getLease();
            BigDecimal bLease = new BigDecimal(lease);
            BigDecimal totalPrice = price.multiply(bLease);

            TbOrder order = new TbOrder();
            order.setId(updateOrderVo.getOrderId());
            order.setLease(lease);
            order.setGoodsId(updateOrderVo.getGoodsId());
            order.setTotalPrice(totalPrice);
            order.setOrderTime(new Date());

            tbOrderMapper.updateByPrimaryKeySelective(order);

            return new MsgVo("200","修改订单成功");
        }
        catch (Exception e){
            return new MsgVo("201","修改订单那异常");
        }
    }

    @Override
    public MsgVo deleteOrder(String orderId) {
        try {
            TbOrder order = new TbOrder();
            order.setId(orderId);
            order.setDeleted(1);
            tbOrderMapper.updateByPrimaryKeySelective(order);
            return new MsgVo("200","删除订单成功");
        }catch (Exception e){
            return new MsgVo("201","删除订单异常");
        }
    }
}
